package com.example.reference_book.service.impl;

import com.example.reference_book.entity.ProhibitedWord;
import com.example.reference_book.dao.ProhibitedWordDao;
import com.example.reference_book.service.ProhibitedWordService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;

/**
 * 违禁词表(ProhibitedWord)表服务实现类
 *
 * @author makejava
 * @since 2022-06-07 22:19:04
 */
@Service("prohibitedWordService")
public class ProhibitedWordServiceImpl implements ProhibitedWordService {
    @Resource
    private ProhibitedWordDao prohibitedWordDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ProhibitedWord queryById(Integer id) {
        return this.prohibitedWordDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param prohibitedWord 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<ProhibitedWord> queryByPage(ProhibitedWord prohibitedWord, PageRequest pageRequest) {
        long total = this.prohibitedWordDao.count(prohibitedWord);
        return new PageImpl<>(this.prohibitedWordDao.queryAllByLimit(prohibitedWord, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param prohibitedWord 实例对象
     * @return 实例对象
     */
    @Override
    public ProhibitedWord insert(ProhibitedWord prohibitedWord) {
        this.prohibitedWordDao.insert(prohibitedWord);
        return prohibitedWord;
    }

    /**
     * 修改数据
     *
     * @param prohibitedWord 实例对象
     * @return 实例对象
     */
    @Override
    public ProhibitedWord update(ProhibitedWord prohibitedWord) {
        this.prohibitedWordDao.update(prohibitedWord);
        return this.queryById(prohibitedWord.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.prohibitedWordDao.deleteById(id) > 0;
    }
}
